package com.hk.utils;

import jakarta.servlet.http.HttpServletRequest;

public class IpUtils {
    /**
     * 获取客户端真实IP（处理代理转发情况）
     */
    public static String getClientIp(HttpServletRequest request) {
        String xffHeader = request.getHeader("X-Forwarded-For");
        if (xffHeader == null) {
            return request.getRemoteAddr(); // 没有代理时直接返回IP
        }
        // 处理多级代理的情况，取第一个非unknown的IP
        String[] ips = xffHeader.split(",");
        for (String ip : ips) {
            if (!"unknown".equalsIgnoreCase(ip)) {
                return ip.trim();
            }
        }
        return request.getRemoteAddr();
    }
}